<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>馆藏图书</title>
    <!--  引入核心皮肤-->
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <!--   引入核心js-->
    <script src="../../layui/layui.js"></script>
    <script type="text/html" id="bar" lay-filter="test">
        <button type="button" class="layui-btn layui-btn-normal" lay-event="chakan">查看解析</button>
        <button type="button" class="layui-btn layui-btn-danger" lay-event="kaishi">开始答题</button>
    </script>
    <script >
        layui.use(['layer','jquery','table','form'],function () {
            let layer=layui.layer;//定义组件
            table=layui.table;
            let $=layui.jquery;//定义组件

            table.render({
                elem: '#testinfo'
                , height: 312
                , url: '/student/selectbystuid' //数据接口
                , parseData: function (res) {
                    return {
                        "code": 0
                        , "msg": "暂无数据"
                        , "data": res
                    }
                }
                , cols: [[ //表头
                    {field: 'questionid', title: '试卷编号'}
                    , {field: 'questionname', title: '科目'}
                    , {field: 'questionname', title: '试卷名称'}
                    , {field: 'operation', title: '操作', toolbar: "#bar"}
                ]]
            });
            table.on('tool(test)', function (obj) {
                var text=0;
                var data = obj.data; //获得当前行数据
                if(obj.event==="kaishi") {
                    $.ajax({
                        type:'get',
                        url:'/student/session',
                        dataType:'json',
                        data:{
                            questionid:data.questionid
                        },
                        async:false,
                        success:function(data){
                            layer.open({
                                type: 2,
                                title:"正在考试",
                                closeBtn: 0,
                                area: ['1000px', '800px'],
                                content: 'exam.html',
                                btn: ['放弃考试'],
                                yes: function (index) {
                                    text=1;
                                    layer.close(index);
                                }
                                , cancel: function (index) {
                                    text=1;
                                    layer.close(index)
                                    return false
                                }
                            });
                        }
                    });

                }else if(obj.event==="chakan"){
                    if(text){
                        layer.open({
                            title: '查看解析',
                            type: 2,
                            content: "myhquestion.html",
                            area: ['800px','800px'],
                            success: function (layero, index) {
                                // 获取子页面的iframe
                                var iframe = window['layui-layer-iframe' + index];
                                // 向子页面的全局函数child传参
                                iframe.child(data);
                            }
                        });
                    }else {
                        alert("请先开始考试")
                    }


                }
            });
        })

    </script>

</head>
<body>

<div>
    <table id="testinfo" lay-filter="test"></table>
</div>
</body>
</html>